package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.dao.UserDao;
import com.project.service.LogService;
import com.project.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户
 */

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, UserBean> implements UserService {
   @Resource
    private LogService logService;
    @Override
    public UserBean login(String userName, String userPwd) {
        QueryWrapper<UserBean> wrapper = new QueryWrapper();
        wrapper.eq("f_userName",userName);
        wrapper.eq("f_userPwd",userPwd);
        UserBean userBean = this.getOne(wrapper);
        System.out.println(userBean);
        if (userBean!=null){
            LogBean logBean1=new LogBean("用户"+userBean.getUserName()+"在"+ LocalTime.now()+"登录系统");
            System.out.println(logBean1);
            logService.addLog(logBean1);
        }
        return userBean;
    }

    @Override
    public IPage<UserBean> findAllUser(int pageNo, int pageSize) {
        Page<UserBean> page = new Page<>(pageNo,pageSize);
        IPage<UserBean> iPage = this.page(page);
        return iPage;
    }

    @Override
    public void addUser(UserBean user) {
        List<UserBean> list = new ArrayList<>();
        for (UserBean i :list){
            if (user.getUserName().equals(i.getUserName())){
                return;
            }
        }
        this.baseMapper.addUser(user);
    }

    /**
     * 添加用户时判断重复
//     * @param userName
     * @return
     */
//    @Override
//    public UserBean repetition(String userName) {
//        QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
//        wrapper.eq("f_userName",userName);
//        return this.getOne(wrapper);
//    }

    @Override
    public void delUser(int userId) {
        this.baseMapper.delUser(userId);

    }

    @Override
    public void updateUser(int userId, String userPwd, String userGrade) {
        this.baseMapper.updateUser(userId,userPwd,userGrade);
    }

    @Override
    public IPage<UserBean> findGrade(int pageNo,int pageSize,String userGrade) {
        Page<UserBean> page = new Page<>(pageNo,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        if (userGrade!=null && !userGrade.equals("")){
            wrapper.eq("f_userGrade",userGrade);
        }
        IPage<UserBean> iPage = this.page(page,wrapper);
        return iPage;

    }

    @Override
    public UserBean findId(int userId) {
        return this.baseMapper.selectById(userId);
    }
}
